function varargout = simul(varargin)
% SIMUL M-file for simul.fig
%      SIMUL, by itself, creates a new SIMUL or raises the existing
%      singleton*.
%
%      H = SIMUL returns the handle to a new SIMUL or the handle to
%      the existing singleton*.
%
%      SIMUL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SIMUL.M with the given input arguments.
%
%      SIMUL('Property','Value',...) creates a new SIMUL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before simul_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to simul_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help simul

% Last Modified by GUIDE v2.5 12-Apr-2012 12:20:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @simul_OpeningFcn, ...
                   'gui_OutputFcn',  @simul_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before simul is made visible.
function simul_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to simul (see VARARGIN)

% Choose default command line output for simul
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes simul wait for user response (see UIRESUME)
% uiwait(handles.figure1);
global data;
if (data.param.cust_temp==0 && data.param.temporal==0)
    set(handles.time_panel,'Visible','Off');
    set(handles.text_run,'Visible','On');
else
    set(handles.time_panel,'Visible','On');
    set(handles.text_run,'Visible','Off');
end


% --- Outputs from this function are returned to the command line.
function varargout = simul_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in pum_ts.
function pum_ts_Callback(hObject, eventdata, handles)
% hObject    handle to pum_ts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns pum_ts contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pum_ts


% --- Executes during object creation, after setting all properties.
function pum_ts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pum_ts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_st_Callback(hObject, eventdata, handles)
% hObject    handle to edit_st (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_st as text
%        str2double(get(hObject,'String')) returns contents of edit_st as a double


% --- Executes during object creation, after setting all properties.
function edit_st_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_st (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_tt_Callback(hObject, eventdata, handles)
% hObject    handle to edit_tt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_tt as text
%        str2double(get(hObject,'String')) returns contents of edit_tt as a double


% --- Executes during object creation, after setting all properties.
function edit_tt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_tt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in cb_vec.
function cb_vec_Callback(hObject, eventdata, handles)
% hObject    handle to cb_vec (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of cb_vec


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;
close simul
if (data.param.Gtype_geo{1} == 1)
    bc_1D
elseif (data.param.Gtype_geo{1} == 2)
    bc_2D
end

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;
time_sh=get(handles.pum_ts,'Value');
correct=0;
if isempty(str2num(get(handles.edit_st,'String'))) && (data.param.cust_temp || data.param.temporal)
    warndlg('Step time must be specified');
    correct=0;
else
    step_time=str2num(get(handles.edit_st,'String'));
    correct=1;
end
if isempty(str2num(get(handles.edit_tt,'String')))  && (data.param.cust_temp || data.param.temporal)
    warndlg('Total time must be specified');
    correct=0;
else
    tot_time=str2num(get(handles.edit_tt,'String'));
    correct=1;
end
vect=false;
if (get(handles.cb_vec,'Value'))
    vect=true;
end
if (correct)
    data.param.set_param_run(time_sh,step_time,tot_time,vect);
end

%verifico y me traigo las constantes custom
t = 0;
addpath(get(data.param,'Gdirec'));

nnod = data.mesh.nnod;
if data.param.cust_diff
    nodal_diffusion = custom_diffusion(xnod,t);
    if (size(nodal_diffusion,1)==nnod && size(nodal_diffusion,2)==1)
       set(data.mesh.nodes,{'k'},num2cell(nodal_diffusion));
    else
        fprintf('Error en custom diffusion - tamanios incorrectos');
    end
else
    set(data.mesh.nodes,{'k'},num2cell(data.param.diffusion*ones(nnod,1)));
end
    
if get(data.param,'cust_conv')
    nodal_convection = custom_convection(xnod,t);
    if (size(nodal_convection,1)==nnod && size(nodal_convection,2)==get(data.mesh,'ndm'))
       set(data.mesh.nodes,{'ux'},num2cell(nodal_convection(:,1)));
       if get(data.mesh,'ndm')==2
           set(data.mesh.nodes,{'uy'},num2cell(nodal_convection(:,2)));
       end
    else
        fprintf('Error en custom convection - tamanios incorrectos');
    end
end
if get(data.param,'cust_sour')
    nodal_source = custom_source(xnod,t);
    if (size(nodal_source,1)==nnod && size(nodal_source,2)==1)
       set(data.mesh.nodes,{'s'},num2cell(nodal_source));
    else
        fprintf('Error en custom source - tamanios incorrectos');
    end
else
    set(data.mesh.nodes,{'s'},num2cell(data.param.source*ones(nnod,1)));
end

if get(data.param,'cust_reac')
    nodal_reaction = custom_reaction(xnod,t);
    if (size(nodal_reaction,1)==nnod && size(nodal_reaction,2)==1)
       set(data.mesh.nodes,{'r'},num2cell(nodal_reaction));
    else
        fprintf('Error en custom reaction - tamanios incorrectos');
    end
else
    set(data.mesh.nodes,{'r'},num2cell(data.param.reaction*ones(nnod,1)));
end

%tomar los patches y llevar la info a los nodos y elementos

%elijo las funciones de forma
data.sF = shapeFunction;

%Paso a resolver el problema
data.mesh.assemble();
%aqui podria mostrarse el resultado de las matrices y demás
data.mesh.solve();




